import { Sync, Random } from "../../../core/utils";
import CCCharacter from "../../eggy/cc_game/cc_character";
import CCBomb from "../cc_game/cc_bomb";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CCCharacterBomb extends CCCharacter { 
    bomb: CCBomb = null;
 
    getNextChar() {
        let msg = {
            src: this,
            dst: null
        };
        this.emit("get-next-char", msg);
        return msg.dst;
    }
    throw() {
        let bomb = this.bomb;
        if (!bomb) {
            return;
        }
        this.ungrab();
        bomb.flyTo(this.getNextChar());
    }
    grab(bomb: CCBomb) {
        this.bomb = bomb;
        bomb.node.setPosition(this.node.position);
        bomb.node.y += 200; 
        this.bomb.on("blowup", this.onBlowup, this);
        this.ani.play("eggy_grabed");

        if (this.character.data.ai) {
            Sync.DelayTime(Random.range(5, 10) / 10).then(() => {
                this.throw()
            });
        }
    }
    onBlowup() {
        this.ungrab();
    }
    ungrab() {
        this.ani.play("eggy_ungrabed");
        this.bomb = null;
    }

    black() {
        this.colorNodes.forEach(node => {
            node.color = cc.Color.BLACK;
        });
        this.face.color = cc.Color.BLACK;
    }
    die() {
        this.isDead = true;
        this.black();
        cc.tween(this.node).delay(1.0).to(1, { opacity: 0 }).start();
    }
};